package com.zbkj.crmeb.member.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.member.model.MemberPointsManagement;
import com.zbkj.crmeb.member.service.IMemberPointsManagementService;
import com.zbkj.crmeb.merchant.admin.controller.AdminBaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员积分管理 前端控制器
 * </p>
 *
 * @author xx
 * @since 2022-05-10
 */
@RestController
@RequestMapping("api/admin/mer/member/points")
@Api(tags = "商户会员积分管理")
public class MemberPointsManagementController extends AdminBaseController {
    @Autowired
    private IMemberPointsManagementService memberPointsManagementService;

    @ApiOperation(value = "会员积分管理配置编辑")
    @RequestMapping(value = "/addMemberBenefits", method = RequestMethod.POST)
    public CommonResult<Object> addMemberBenefits(@RequestBody @Validated MemberPointsManagement memberPointsManagement, HttpServletRequest servletRequest){
        memberPointsManagement.setMerId(getMerId(servletRequest));
        return memberPointsManagementService.addOrUpd(memberPointsManagement) ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "会员积分管理配置查询")
    @RequestMapping(value = "/getPointsManagement", method = RequestMethod.GET)
    public CommonResult<MemberPointsManagement> getPointsManagement(HttpServletRequest servletRequest){
        return CommonResult.success(memberPointsManagementService.getOne(Wrappers.lambdaQuery(MemberPointsManagement.class).eq(MemberPointsManagement::getMerId,getMerId(servletRequest))));
    }

    @PutMapping("/{flag}")
    @ApiOperation(value = "开启/关闭积分开关")
    public CommonResult<Boolean> setEnablePointsConfig(@PathVariable Boolean flag, HttpServletRequest request) {
        return CommonResult.success(memberPointsManagementService.setEnablePointsConfig(flag, getMerId(request)));
    }

}

